package hn.sigit.logic.bpm;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

@Name("confProceso")
@Scope(ScopeType.APPLICATION)
@AutoCreate
public class ConfiguracionProcesosImpl {
	private ServletContext servletContext;
	private File configFile;
	DocumentBuilderFactory dbf;
	DocumentBuilder db;
	Document doc;
	String appName;
	
	private Map<String, String> taskToView;
	
	
	private static String createKey(String appName, String processName, String taskName) {
		return appName + "$$$" + processName + "$$$" + taskName;
	}
	
	public ConfiguracionProcesosImpl() {
	}
	
	@Create
	public void init() {
		servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
		configFile = new File(servletContext.getRealPath("/WEB-INF/process-view-map.xml"));
		taskToView = Collections.synchronizedMap(new HashMap<String, String>());
		appName = servletContext.getServletContextName();
		String processName;
		
		dbf = DocumentBuilderFactory.newInstance();
		
		try {
			db = dbf.newDocumentBuilder();
			doc = db.parse(configFile);
			doc.getDocumentElement().normalize();
			NodeList processList = doc.getElementsByTagName("process");
			for (int p = 0; p < processList.getLength(); p++) {
				Node processNode = processList.item(p);
				if (processNode.getNodeType() == Node.ELEMENT_NODE) {
					Element processElem = (Element) processNode;
					
					Node nameNode = processElem.getElementsByTagName("name").item(0);
					Node mappingsNode = processElem.getElementsByTagName("mappings").item(0);
					
					processName = nameNode.getFirstChild().getNodeValue();
					
					NodeList mappingsList = mappingsNode.getChildNodes();
					for (int m = 0; m < mappingsList.getLength(); m++) {
						Node mappingNode = mappingsList.item(m);
						if (mappingNode.getNodeType() == Node.ELEMENT_NODE) {
							Element mappingElem = (Element) mappingNode;
							
							Node taskNameNode = mappingElem.getElementsByTagName("task-name").item(0);
							String taskName = taskNameNode.getFirstChild().getNodeValue();
							
							Node viewIdNode = mappingElem.getElementsByTagName("view-id").item(0);
							String viewId = viewIdNode.getFirstChild().getNodeValue();
							
							taskToView.put(createKey(appName, processName, taskName), viewId);
						}
					}
				}
			}
			
		} catch (ParserConfigurationException pce) {
			pce.printStackTrace();
			System.err.println("ERROR de configuracion del parser XML");
		} catch (SAXException e) {
			e.printStackTrace();
			System.err.println("ERROR al parsear documento XML");
		} catch (IOException e) {
			e.printStackTrace();
			System.err.println("ERROR al abrir documento XML, process-view-map.xml");
		}
	}
	
	public String getViewId(String processName, String taskName) {
		return taskToView.get(createKey(appName, processName, taskName));
	}	
}
